<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\User;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Fenyongdaili extends Command
{
    protected function configure()
    {
        $this->setName('fenyong_daili')->setHelp('用户端分佣')->setDescription('用户端订单分佣');

    }

    protected function execute(Input $input, Output $output)
    {
        //is_agency
        $data         = \app\common\model\Order::where(['order_status'=>4,'is_agency'=>2])->where('jiesuan_time','<',time())->select();
        $packageModel = new Packages();
        $userModel    = new User();
        if($data){
            $day_7 = 7*86400+time();;
            foreach ($data as $k=>$v){
                if($v->jiesuan_time == 0){
                    continue;
                }
                $packageInfo = $packageModel->where(['id'=>$v['goods_id']])->find();
                if(!$packageInfo){
                    continue;
                }
                $v->settlement_time = time();
                $v->order_status    = 5;
                if($v->is_more == 2){
                    $v->more_num = 1;
                    $v->two_js_time      = settlement_time($v->settlement_time);//二次结算时间;
                    $huafei = $packageInfo['bxhf_text'];
                    if($v->hexiao_next_time > 0 && time() < $v->hexiao_next_time){
                        $v->is_hexiao = 5;
                    }
                    if($packageInfo['day'] != 6){
                        $v->hexiao_next_time = $day_7;
                    }else{
                        $v->hexiao_next_time = $v->two_js_time;
                    }
                    jiangli($v,$packageInfo);
                    if($v->parent_id){
                        jiangli($v,$packageInfo,2);
                    }
                    if($v->two_id){
                        jiangli($v,$packageInfo,3);
                    }
                    //区县
                    if($v->agency_area_id){
                        jiangli($v,$packageInfo,4);
                    }
                    //小区
                    if($v->plot_id){
                        jiangli($v,$packageInfo,5);
                    }
                }else{
                    $huafei = bcadd($packageInfo['ciyue_set_meal'], $packageInfo['bxhf_text']);
                    if($v->hexiao_next_time > 0 && time() < $v->hexiao_next_time){
                        $v->is_hexiao = 2;
                    }
                    if($packageInfo['one_commission']){
                        $userModel::money($packageInfo['one_commission'], $v->user_id, '自购奖励',$v->id,
                            $packageInfo['id']);
                    }

                    if($packageInfo['junior_contribution'] && $v['parent_id']){
                        $userModel::money($packageInfo['junior_contribution'], $v->parent_id, '直推奖励', $v->id,
                            $packageInfo['id']);
                    }

                    if($packageInfo['junior_contribution_tow'] && $v['two_id']){
                        $userModel::money($packageInfo['junior_contribution_tow'], $v->two_id, '下下级贡献', $v->id,
                            $packageInfo['id']);
                    }

                    if($packageInfo['pplots_yunying_profit'] && $v['plot_id']){
                        $userModel::money($packageInfo['pplots_yunying_profit'], $v->plot_id, '小区运营分润', $v->id,
                            $packageInfo['id']);
                    }

                    if($packageInfo['pplots_profit'] && $v['agency_area_id']){
                        $userModel::money($packageInfo['pplots_profit'], $v->agency_area_id, '区县运营分润', $v->id,
                            $packageInfo['id']);
                    }
                }
                if($huafei){
                    $userModel::money($huafei, $v['user_id'], '话费报销', $v->id,
                        $packageInfo['id']);
                }
                $v->save();
                echo "操作成功";
            }
        }
    }
}
